package coreconcurrency.blocking;

import java.io.File;
import java.util.concurrent.BlockingQueue;

/**
 * create by yongli on 2022-03-03 21:16
 */
class FileEnumerationTask implements Runnable{
    public static File DUMMY =new File(""); //空文件。
    private BlockingQueue<File> bq;
    private File file;
    public FileEnumerationTask(BlockingQueue<File> bq,String filePath){
        this.bq= bq;
        this.file = new File(filePath);
    }
    @Override
    public void run() {
        try{
            product(file);
            bq.put(DUMMY);
        }catch (InterruptedException e){

        }

    }
    public void product(File file) throws InterruptedException{
        File[] files =file.listFiles();
        for (File file1:files) {
            if (file1.isDirectory()) {
                product(file1);
            }
            else{
                // put方法线程会发生阻塞。
                bq.put(file1);
                System.out.println("放入一个文件："+file1.getName());
            }
        }
    }
}
